package StringAlgorithm;

import java.util.LinkedList;
import java.util.Queue;

public class leet_415 {
    public static void main(String[] args) {
        Solution_415 p = new Solution_415();
        String num1 = "456";
        String num2 = "77";

        String result = p.addStrings(num1, num2);
        System.out.println(result);
    }

}

class Solution_415 {
    public String addStrings(String num1, String num2) {
        Queue<Character> q1 = new LinkedList<>();
        Queue<Character> q2 = new LinkedList<>();

        // 将2个string的每一个字符放入队列中
        for(int i=num1.length()-1; i>-1; i--){
            q1.offer(num1.charAt(i));
        }

        for(int i=num2.length()-1; i>-1; i--){
            q2.offer(num2.charAt(i));
        }
        String result = addQueue(q1, q2);
        return result;
    }

    public String addQueue(Queue q1, Queue q2){
        // 结果字符
        String result = "";
        // 每个位置的进制数
        int Divisor = 0;
        while(!q1.isEmpty() || !q2.isEmpty()){
            int num1 = 0;
            int num2 = 0;
            // 将2个队列的值依次抛出
            if(!q1.isEmpty()){
               num1 =  Integer.parseInt(String.valueOf(q1.poll()));
            }
            if(!q2.isEmpty()){
                num2 =  Integer.parseInt(String.valueOf(q2.poll()));
            }

            // 结果数
            int allNum = Divisor + num1 + num2;
            int Remainder = allNum%10;
            Divisor = allNum/10;
            result = Remainder + result;
        }
        if(Divisor!=0){
            result = Divisor + result;
        }
        return result;
    }
}
